var EXPORTED_SYMBOLS = ["findBestInputField"];

// find the one input element in this doc and it frames.
function findSoleInputElement(doc) {
}


function findBestInputField(doc, _logMessage) {
  // <input type="text" name="openid_url" id="openid_url"
  var inputField = doc.getElementById("openid_url");
  if (inputField) {
    return inputField;
  }
//  _logMessage("urlbarClick no id-openid_url field in " + doc.location.href);
  var inputFields = doc.getElementsByName("openid_url");
  inputField = inputFields.item(0);
  if (inputField) {
    return inputField;
  }
//  _logMessage("urlbarClick no name-openid_url field in " + doc.location.href);
  inputField = doc.getElementById("rawOpenId");
  if (inputField) {
    return inputField;
  }
//  _logMessage("urlbarClick no rawOpenId field in " + doc.location.href);
  inputField = doc.getElementById("openid_identifier");
  if (inputField) {
    return inputField;
  }
//  _logMessage("urlbarClick no openid_identifier field in " + doc.location.href);
  inputFields = doc.getElementsByClassName("user_id"); // JANRAIN
  if (inputFields) {
    inputField = inputFields.item(0);
    if (inputField) {
      if ((inputField.tagName == "input") || (inputField.tagName == "INPUT")) {
        return inputField;
      }
    }
  }
//  _logMessage("urlbarClick no class user_id field in " + doc.location.href);
  var curElement = doc.activeElement;
  if (curElement) {
//    _logMessage("urlbarClick curElement.tagName=" + curElement.tagName);
    if ((curElement.tagName == "input") || (curElement.tagName == "INPUT")) {
      return curElement;
    }
  }
  var focused = (doc.commandDispatcher) ? doc.commandDispatcher.focusedElement : null;
  if (focused) {
//    _logMessage("urlbarClick focused.tagName=" + focused.tagName);
//    _logMessage("urlbarClick focused.id=" + focused.id);
    if (focused.tagName == "INPUT") {
      return focused;
    } 
  }
  var oneInputfield = findSoleInputElement(doc);
  if (oneInputfield) {
    inputField = oneInputfield;
  }
  
  var frames = doc.defaultView.frames;
//  _logMessage("urlbarClick frames.length=" + frames.length);
  for (var i = 0; i < frames.length; i++) {
    var frame = frames[i];
    if (!frame) continue;
    if (!frame.document) continue;
    inputField = findBestInputField(frame.document, _logMessage);
    if (inputField) {
      return inputField;
    }
  }
}